<template>

  <el-container class="home-container">
    <el-header>
      <div>
        <h1>TheraPie<sup class="reg">®</sup></h1>
        <h3>后台管理系统</h3>
      </div>
      <el-button type="info" @click="logout" size="mini">退出</el-button>
    </el-header>

    <el-container>

      <el-aside width="isCollapse ? '65px' : '201px' ">
        <!-- collapse-transition 是否开启折叠动画 -->
        <!-- router Element-Ui是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
        <el-menu :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" :router="true" default-active="/users">
          <div class="toggle-button" @click="toggleCollapse"><i class="el-icon-more-outline"></i></div>
          <!-- 一级菜单 -->
          <!-- index只接收字符串，加空字符转为字符串 -->
          <el-submenu :index="menu.id + '' " v-for="menu in menuList" :key="menu.id">
            <template slot="title">
              <i :class="iconObj[menu.id]"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index=" '/' + subMenu.path " v-for="subMenu in menu.children" :key="subMenu.id" @click="saveNavState('/'+subMenu.path)">
              <i class="el-icon-menu"></i>
              <span>{{ subMenu.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>

    </el-container>
  </el-container>

</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        '125': 'el-icon-user',
        '103': 'el-icon-suitcase',
        '101': 'el-icon-goods',
        '102': 'el-icon-document',
        '145': 'el-icon-data-line',
      },
      isCollapse: false,  // 是否折叠
      activePath: '',     // 被激活链接地址
    }
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push('/login'); 
    },
    async getMenuList() { // 获取所有菜单
      const { data: res } = await this.$http.get('menus');
      // console.log(res);
      if(res.meta.status != 200) return this.$message.error(res.data.meta);
      this.menuList = res.data;
    },
    toggleCollapse() {  // 点击按钮，菜单折叠展开
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {  // 保存连接激活状态
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    }
  }
}
</script>
<style lang="less" scoped>
  @font-face {
    font-family: "futuraltBold";
    src: url("../assets/font/futuralt bold.otf");
  }
  .home-container{height: 100%;}
  .el-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #ccc;
    > div{display: flex; align-items: center;
      h1{font-size: 26px; font-family: "futuraltBold";
        .reg { font-size: 14px; vertical-align: text-top;}
      }
      h3{margin-left: 10px; font-weight: normal;}
    }
  }
  .el-aside{border-right: 1px solid #ccc;
    .el-menu{border-right: none}
    .toggle-button{text-align: right; background-color: #efefef; font-size: 10px; line-height: 3em; padding-right: 10px; cursor: pointer;}
  }
  .el-main{background-color: #efefef;}
</style>